set more off
capture log close
capture clear all
*log using National_banks_crisis5.log, text replace
set memory 200m

/***!***!***!***!***!*** [National_banks_crisis_2018_b.do ] ***!***!***!***!***!
*
* Project: National Banks  		
* Programmer:  Scott Fulford, 
*
* Date:    	 August 2018
*
* Auditor:      
* Audit Date:   
*
* Purpose:      
* 1) Create time series of variables used in VAR
* 2) 
* 3)
* 		      
*
* Ouputs: 
*		
* Sources: y.xlsx created from sources described in the paper
Felipe writes: 
The variables are in the file y.xls, within the folder "national banks peg/code/matlab favar"

The variables are (in order):

A) time index
B) year
C) month
D) factory employment
E) Business failures
F) Pig iron
G) industrial production 

Felipe notes:
The last column (column I) is the number of days between periods. Before running things I divide the variables by those number of days to keep things comparable.

*
***!***!***!***!***!***!***!***!***!***!***!***!***!***!***!***!***/


/***Define Global Directory ****/
//Change rootdir to where store files
	global ROOTDIR "F:/Dropbox/national banks peg/Replication_files"
	local PROGDIR  "$ROOTDIR"
	local INDIR  "$ROOTDIR/data"
	local OUTDIR  "$ROOTDIR/data"
/*******************************/

cd "`INDIR'"

/**********
Load data used to make VAR

************/


tempfile readin
local xx =0
foreach thisvar in "business failures" "pig iron" "factory employment" "industrial production" "industrial production smoothed" {
import excel "counterfactuals_output.xlsx", first sheet("`thisvar'")  clear
gen date = ym(year,month)
order date
format date %tmCCYY!mnn
tsset date
  if "`thisvar'" == "business failures" {
    local thisvarname "business_failures"
    local thislabnam "Business failures"
  }
  if "`thisvar'" == "pig iron" {
    local thisvarname "pig_iron"
    local thislabnam "Pig iron"
  }
  if "`thisvar'" == "factory employment" {
    local thisvarname "factory_employment"
    local thislabnam "Factory employment"
  }
  if "`thisvar'" == "industrial production" {
    local thisvarname "industrial_production"
    local thislabnam "Industrial production"
  }
  if "`thisvar'" == "industrial production smoothed" {
    local thisvarname "ind_prod_smth"
    local thislabnam "Industrial production smoothed"
  }
  rename series `thisvarname'
  rename counterfactual1 cf1_`thisvarname'
  rename counterfactual2 cf2_`thisvarname'
  label var `thisvarname' "`thisvar'"
  gen cf1_diff_`thisvarname' = cf1_`thisvarname'-`thisvarname'
    
    /* Create both fast and slow, not difference, for appendix 
   twoway  (line   `thisvarname' date, lcolor(gs10) lwidth(medthick)) ///
          (line   cf1_`thisvarname' date, lpattern(dash) lcolor(orange)) ///
          (line   cf2_`thisvarname' date, lpattern(shortdash) lcolor(green) ) , ///
    tmtick(1880m1(12)1910m1) ///
    title("`thislabnam'") ///
    name( `thisvarname', replace) ///
    legend(on cols(3) order( 1 - - - "Cholesky:" 2 3) label(1 "`thislabnam'") label(2 "Fast") label(3 "Slow  ")) ///
    xtitle(Date) 
*/
   /* Create difference, only slow, to include in paper */
   twoway  (line   `thisvarname' date, lcolor(orange) /*lwidth(medthick)*/) ///
          (line   cf1_diff_`thisvarname' date, lpattern(solid) lcolor(green)) ///
          /*(line   cf2_`thisvarname' date, lpattern(shortdash) lcolor(green) ) */ , ///
    tmtick(1880m1(12)1910m1) ///
    title("`thislabnam'") ///
    name( `thisvarname', replace) ///
    legend(on cols(2)  label(1 "`thislabnam'") label(2 "Counterfactual difference    ") ) ///
    xtitle(Date) 
   graph export "`OUTDIR'/VAR_`thisvarname'.pdf", as(pdf) font(times) name(`thisvarname') replace

  if `xx' ==0 {
    save `readin', replace
  }  
  else {
    merge 1:1 date using `readin'
    drop _merge
    save `readin', replace
  }

}

  graph combine factory_employment business_failures pig_iron industrial_production , ///
  imargin(medsmall  ) cols(2)  xsize(9) ysize(6.5  ) iscale(*.8) ///
  name(VAR, replace)
cd "`OUTDIR'"
  graph export VAR_variables_time.pdf, as(pdf) font(times) name(VAR) replace

 // graph export VAR_ind_prod_smooth.pdf, as(pdf) font(times) name(ind_prod_smth) replace

exit
/*
rename A time_index
rename B year
rename C month
gen factory_employment =real(D)
gen business_failures = real(E)
gen pig_iron = real(F)
gen industrial_production = real(G) 
*/


exit
label var business_failures "Business failures"
label var pig_iron "Pig iron"
label var industrial_production "Industrial production"

foreach thisvar in factory_employment business_failures pig_iron industrial_production {
twoway  (line   `thisvar' date ), ///
  tmtick(1880m1(12)1910m1) ///
  name( `thisvar', replace) ///
  xtitle(Date) 
}


  graph combine factory_employment business_failures pig_iron industrial_production, ///
  imargin(small) cols(2)  xsize(9) ysize(6)  ///
  name(VAR, replace)
cd "`OUTDIR'"
  graph export VAR_variables_time.pdf, as(pdf) font(times) name(VAR) replace


exit
 ///   
		(line  business_failures date,  ) ///
		(line  pig_iron date, lwidth(thick) yaxis(1)) 

    if year >=1880, ///
  /// tlabel(01jan1880 01jan1884 01jan1888 01jan1892 01jan1896 01jan1900 01jan1904 01jan1908 01jan1912) ///
  xline(1890 1893 1896 1900 1907, lcolor(gs10)) /// 
  text(.83  1890 "Sherman Silver" "Purchase Act", just(right) placement(w) size(small)) /// 
  text(.83  1893 "1893" "Panic", just(right) placement(w) size(small)) ///
  text(.83  1896 "1896" "Election", just(right) placement(w) size(small))  /// 
  text(.73 1900 "Gold" "Standard" "Act" , just(right) placement(w) size(small)) ///
  text(.73 1907 "1907" "Panic", just(right) placement(w) size(small)) ///
  legend(on) /// 
  xlabel(1880(5)1910) ///
  ytitle("", axis(1)) ///
  name(VAR, replace) ///
  xtitle(Year) 


  
exit
